<?php
namespace app\api\controller;
use app\model\WxAssistant as WxAssistantModel;
use app\model\WxAssistantConfig as WxAssistantConfigModel;
use app\model\Token as TokenModel;
use think\Db;


class WxAssistant extends Base
{   
    protected $noCheckToken = ['index','read'];
    
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $map_params = [
            
        ];
        $map = getSearchParam($map_params);

        $model = new WxAssistantModel();

        $list = $model->where($map)->order('id asc')->select();
        foreach ($list as $k => $vo) {
            // $configs = Db::name('wx_assistant_config')->where('wx_assistant_id',$vo['id'])->select();
            // foreach ($configs as $c_k => $c_v) {
            //     $configs[$c_k] = array_merge($c_v,WxAssistantConfigModel::getTypeConfig($c_v['type']));
            // }
            
            if ($vo['is_config_select'] == 1) {
                $configs = WxAssistantModel::getConfigTypeList();
                $list[$k]['configs'] = $configs;
            }
                
            // if (!empty($configs)) {
            //     $list[$k]['type'] = $configs[0]['type'];
            //     $list[$k]['request_type'] = $configs[0]['request_type'];
            //     $list[$k]['api_url'] = $configs[0]['api_url'];
            //     $list[$k]['api_param'] = $configs[0]['api_param'];
            // }
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 获取详情
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function read($id,$config_id=''){
        $model = new WxAssistantModel();
        if (strpos($id,'member_') !== false) {
            $id = str_replace('member_', '', $id);
            $member = Db::name('wx_member')->where('id',$id)->find();
            $data = $model->where('id',$member['wx_assistant_id'])->find();
            $data['id'] = 'member_'.$member['id'];
            $data['name'] = $member['name'];
            $data['system'] = $member['description'];
            $data['image'] = $member['image'];
        }else{
            $data = $model->where('id',$id)->find();
            // $configs = Db::name('wx_assistant_config')->where('wx_assistant_id',$data['id'])->select();
            // foreach ($configs as $c_k => $c_v) {
            //     $configs[$c_k] = array_merge($c_v,WxAssistantConfigModel::getTypeConfig($c_v['type']));
            // }

            if ($data['is_config_select'] == 1) {
                $configs = WxAssistantModel::getConfigTypeList();
                $data['configs'] = $configs;
            }
            
        }
        return apiResult('2000','获取成功',$data);
    }

   


}
